package com.fdd.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.fdd.dao.BookDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

@PropertySource("classpath:jdbc.properties")
public class JdbcConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    //1.定义一个方法获得要管理的对象
    //2.添加@bean,表示当前函数的返回值是一个bean。
    //3.当@bean方法需要引用类型的形参时，将会尝试自动装配，会去容器中找已注册的bean.
    @Bean
    public DataSource dataSource(BookDao bookDao) {
        System.out.println(bookDao);
        System.out.println("DataSource::DataSource:" + driver + "," + url);
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        return ds;
    }
}
